import { defineStore } from 'pinia'
import { ref } from 'vue'
import { copywritingApi } from '@/api/system/copywriting'

// 定义类型接口
interface ContentType {
  value: string
  label: string
  description: string
}

interface ContentStyle {
  value: string
  label: string
  description: string
}

interface Copywriting {
  content: string
  content_type: string
  style: string
  created_at: string
}

export const useCopywritingStore = defineStore('copywriting', () => {
  const isLoading = ref(false)
  const copywritings = ref<Copywriting[]>([])
  const contentTypes = ref<ContentType[]>([])
  const contentStyles = ref<ContentStyle[]>([])

  // 获取文案类型
  const fetchContentTypes = async () => {
    try {
      const response = await copywritingApi.getTypes()
      if (response.code === 200) {
        contentTypes.value = response.data
      }
    } catch (error) {
      console.error('获取文案类型失败:', error)
    }
  }

  // 获取文案风格
  const fetchContentStyles = async () => {
    try {
      const response = await copywritingApi.getStyles()
      if (response.code === 200) {
        contentStyles.value = response.data
      }
    } catch (error) {
      console.error('获取文案风格失败:', error)
    }
  }

  // 生成文案
  const generateCopywriting = async (params: any) => {
    isLoading.value = true
    try {
      const response = await copywritingApi.generate(params)
      if (response.code === 200) {
        copywritings.value = response.data
        return response
      }
    } catch (error) {
      console.error('生成文案失败:', error)
      throw error
    } finally {
      isLoading.value = false
    }
  }

  return {
    isLoading,
    copywritings,
    contentTypes,
    contentStyles,
    fetchContentTypes,
    fetchContentStyles,
    generateCopywriting
  }
})